package com.lyfy.sd.order.config;

import com.lyfy.sd.order.mycat.MyCatMurmurShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;

/**
 * @author: 谢星星
 * @date: 2022/10/2 01:00
 * @description:
 */
@Slf4j
@MapperScan("com.lyfy.sd.order.dao.mapper")
@Configuration
public class AppConfiguration {


    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }

    @Bean("myCatMurmurShardingAlgorithm")
    public MyCatMurmurShardingAlgorithm myCatMurmurShardingAlgorithm(){
        return new MyCatMurmurShardingAlgorithm(0, 2, 80);
    }

}
